package org.iqiyi.controller;


import com.alicp.jetcache.anno.CacheInvalidate;
import com.alicp.jetcache.anno.CacheType;
import com.alicp.jetcache.anno.CacheUpdate;
import com.alicp.jetcache.anno.Cached;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.google.common.io.Files;
import io.netty.util.ByteProcessor;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.iqiyi.cache.RemoveCache;
import org.iqiyi.entity.VideoEntity;
import org.iqiyi.service.VideoService;
import org.iqiyi.util.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.iqiyi.controller.VideoController;
import org.springframework.web.bind.annotation.RestController;

import javax.json.Json;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author huqi
 * @since 2021-04-17
 */
@RestController
@Api(value = "视频模块", tags = "视频模块")
@RequestMapping("/video")
public class VideoController {
    @Autowired
    private VideoService videoService;

    //@Cached(cacheType = CacheType.REMOTE,name = "VideoController.selectAll:")
    @ApiOperation(value = "查询未禁用和显示中的视频", notes = "查询未禁用和显示中的视频")
    @GetMapping(value = "/selectAll")
    public JsonResult<List<VideoEntity>> selectAll(String typeName) {
        List<VideoEntity> list = videoService.selectAll(typeName);
        return JsonResult.success(list);
    }

    //@RemoveCache(key = "VideoController.selectAll:")
    @ApiOperation(value = "添加视频模块", notes = "添加视频模块")
    @PostMapping(value = "/add")
    public JsonResult insert(VideoEntity videoEntity) {
        int i = videoService.insert(videoEntity);
        return i > 0 ? JsonResult.success() : JsonResult.fail();
    }

    //@RemoveCache(key = "VideoController.selectAll:")
    @ApiOperation(value = "删除或禁用视频", notes = "删除或禁用")
    @PostMapping(value = "/deleteORDisable")
    public JsonResult deleteORDisable(Long id, String dataActive) {
        boolean flag = videoService.update(new UpdateWrapper<VideoEntity>().eq(VideoEntity.ID, id)
                .set(VideoEntity.DATA_ACTIVE, dataActive));
        return flag ? JsonResult.success() : JsonResult.fail();
    }


    @ApiOperation(value = "获取视频流", notes = "获取视频流")
    @GetMapping(value = "/getVideo")
    public void getVideo(Long id, HttpServletResponse response) throws IOException {
        videoService.getVideo(id,response);
    }

}

